package com.mostromas.ess.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mostromas.ess.entities.UserProject;
import com.mostromas.ess.project.mapper.UserProjectsMapper;
import com.mostromas.ess.project.req.UserProjectsREQ;
import com.mostromas.ess.project.service.IUserProjectsService;
import com.mostromas.ess.util.base.Result;
import com.mostromas.ess.util.tools.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserProjectsServiceImpl extends ServiceImpl<UserProjectsMapper, UserProject> implements IUserProjectsService {

    @Override
    public Result queryList(UserProjectsREQ req) {
        return null;
    }

    @Override
    public Result updateEffectiveTime(UserProject info) {
        if(StringUtils.isNotEmpty(info.getId())){
            UserProject up = this.getById(info.getId());
            if(up == null){
                return Result.error("软件ID不正确");
            }
            info.setEffectiveTime(DateUtil.getNextDay(this.getById(info.getId()).getEffectiveTime(),info.getDays()));
        }else{
            info.setEffectiveTime(DateUtil.getNextDay(new Date(),info.getDays()));
        }
        this.saveOrUpdate(info);
        return Result.ok();
    }

    public int checkRegister(String projectId,String hardwareCoding){
        QueryWrapper<UserProject> wrapper =new QueryWrapper<>();
        wrapper.eq("project_id",projectId);
        wrapper.eq("hardware_coding",hardwareCoding);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public List<UserProject> getProjectsByUsername(String username) {
        QueryWrapper<UserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public UserProject checkLogin(String username,String projectId,String hardwareCoding) {
        QueryWrapper<UserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("project_id",projectId);
        wrapper.eq("hardware_coding",hardwareCoding);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public UserProject getInfoByUsernameProjectId(String username, String projectId) {
        QueryWrapper<UserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("project_id",projectId);
        return baseMapper.selectOne(wrapper);
    }
}